//
//  CollectDataBaseManager.m
//  BSLCar
//
//  Created by 梁毅 on 15/5/29.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "CollectDataBaseManager.h"

static sqlite3 *dbPoint = nil;
@implementation CollectDataBaseManager
+ (void)openDataBase{
    if (dbPoint) {
        return ;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"ShouCang.rdb"];
//    NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));
    sqlite3_open(path.UTF8String, &dbPoint);
   

}
+ (void)createTabel{
    [self openDataBase];
    NSString *sql = @"create table shoucang(title varchar(50), dealerprice varchar(20), dealeraddress varchar(50), dealershortname varchar(50))";
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)insertValue:(OnMarketForthModel *)onMark{
    [self openDataBase];
    NSString *sql =[NSString stringWithFormat:@"insert into shoucang values ('%@','%@','%@','%@')",onMark.title ,onMark.dealerprice, onMark.dealeraddress, onMark.dealershortname];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)deleteValue
{
    [self openDataBase];
    NSString *sql = @"delete from shoucang";
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (void)deleteValue:(OnMarketForthModel *)onMark
{
    [self openDataBase];
    NSString *sql = [NSString stringWithFormat:@"delete  from shoucang where  dealershortname = '%@' and dealerprice = '%@'",onMark.dealershortname, onMark.dealerprice];
    sqlite3_exec(dbPoint, sql.UTF8String, NULL, NULL, NULL);
}
+ (NSArray *)selectData
{
    [self openDataBase];
    //创建数据库替身临时保存数据
    sqlite3_stmt *stmt = nil;
    //创建sql语句
    NSString * sql = @"select * from shoucang";
    //执行语句
    int result = sqlite3_prepare_v2(dbPoint, sql.UTF8String, -1, &stmt, NULL);
    //判断是否成功
    if (result == SQLITE_OK) {
        //创建数组来保存查询的数据
        NSMutableArray *array = [NSMutableArray array];
        //如果还有下一行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //获取数据
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *dealerprice = sqlite3_column_text(stmt, 1);
            const unsigned char *dealeraddress = sqlite3_column_text(stmt, 2);
            const unsigned char *dealershortname = sqlite3_column_text(stmt, 3);
            //封装成model
            OnMarketForthModel *model = [[OnMarketForthModel alloc]init];
            model.dealershortname = [NSString stringWithUTF8String:(const char *)dealershortname];
            model.title = [NSString stringWithUTF8String:(const char *)title];
            model.dealeraddress = [NSString stringWithUTF8String:(const char *)dealeraddress];
            model.dealerprice = [NSString stringWithUTF8String:(const char *)dealerprice];
            [array addObject:model];
        }
        //删掉替身
        sqlite3_finalize(stmt);
        return array;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}

@end
